import grpc

from grpc_error_test.proto import hello_pb2_grpc, hello_pb2

if __name__ == '__main__':
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = hello_pb2_grpc.GreeterStub(channel)
        hello_request = hello_pb2.HelloRequest()
        hello_request.name = 'bobby'
        try:
            # context
            rsp: hello_pb2.HelloReply = stub.SayHello(hello_request, timeout=3)
        except grpc.RpcError as e:
            d = e.details()
            status_code = e.code()
            print(status_code.name)
            print(status_code.value)
            print(f"code：{status_code.name}, detail: {d}")
